package com.suman.bbcticker.weather;

import java.net.URL;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.util.Log;
import android.widget.RemoteViews;

import com.suman.bbcticker.R;

/** Useful Utility in working with temperatures. (conversions). */
public class WeatherUtils {
	
	private static final String TAG = "WeatherUtils";

	public static int fahrenheitToCelsius(int tFahrenheit) {
		return (int) ((5.0f / 9.0f) * (tFahrenheit - 32));
	}

	public static int celsiusToFahrenheit(int tCelsius) {
		return (int) ((9.0f / 5.0f) * tCelsius + 32);
	}
	
	public void invokeGoogleWeatherServiceAndUpdateWidget(double latitude,double longitude, Context mContext, int mAppWidgetId,String locationName){
		try {
			
		
		String queryString = getQueryStringBasedOnCurrentLocation(latitude,longitude);

		URL url = new URL(queryString);

		/* Get a SAXParser from the SAXPArserFactory. */
		SAXParserFactory spf = SAXParserFactory.newInstance();
		SAXParser sp = spf.newSAXParser();

		/* Get the XMLReader of the SAXParser we created. */
		XMLReader xr = sp.getXMLReader();

		/*
		 * Create a new ContentHandler and apply it to the XML-Reader
		 */
		GoogleWeatherHandler gwh = new GoogleWeatherHandler();
		xr.setContentHandler(gwh);

		/* Parse the xml-data our URL-call returned. */
		xr.parse(new InputSource(url.openStream()));

		/* Our Handler now provides the parsed weather-data to us. */
		WeatherSet weatherSet = gwh.getWeatherSet();

		RemoteViews remoteViews = new RemoteViews(mContext.getPackageName(), R.layout.widget_word);
		remoteViews.setTextViewText(R.id.temperature, Integer.toString(weatherSet.getWeatherCurrentCondition().getTempCelcius()) + "-->" + locationName);
		AppWidgetManager manager = AppWidgetManager.getInstance(mContext);
		manager.updateAppWidget(mAppWidgetId, remoteViews);

		Log.e(TAG, "onLocationChanged getTempCelcius.." + weatherSet.getWeatherCurrentCondition().getTempCelcius());
		} catch (Throwable t) {
			t.printStackTrace();
			// TODO: handle exception
		}
	}
	

	private String getQueryStringBasedOnCurrentLocation(Double latitude,Double longitude) {
		String latitudeString = String.valueOf(latitude);
		String longitudeString = String.valueOf(longitude);
		Log.e(TAG, "latitudeString is...longitudeString is.." + latitudeString + ".." + longitudeString );
		String dotReplacedLatitude = latitudeString.replace(".", "");
		String dotReplacedLongitude = longitudeString.replace(".", "");
		
		String dotMinusReplacedLatitude = dotReplacedLatitude.replace("-", "");
		String dotMinusReplacedLongitude = dotReplacedLongitude.replace("-", "");
		int latitudeStringLength = dotMinusReplacedLatitude.length();
		int longitudeStringLength = dotMinusReplacedLongitude.length();
		dotReplacedLatitude = zeroCorrection(dotReplacedLatitude, latitudeStringLength);		
		dotReplacedLongitude = zeroCorrection(dotReplacedLongitude, longitudeStringLength);
		Log.e(TAG, "dotReplacedLatitude and dotReplacedLongitude..."+dotReplacedLatitude + "--"+ dotReplacedLongitude);
		
		CharSequence latitudee6 = dotReplacedLatitude.subSequence(0, 8);
		CharSequence longitudee6 = dotReplacedLongitude.subSequence(0, 8);
		
		String queryString = "http://www.google.com/ig/api?weather=,,," + latitudee6 + "," + longitudee6;
		Log.e(TAG, "URL is..." + queryString);
		return queryString;
	}

	private String zeroCorrection(String dotReplacedLatitude, int latitudeStringLength) {
		Log.e(TAG, "zeroCorrection is...dotReplacedLatitude..." + dotReplacedLatitude + "latitudeStringLength--"+latitudeStringLength);
		
		if(latitudeStringLength<8){
			int noOfZerorsToBeAdded = 8-latitudeStringLength;
			for(int i=0;i<noOfZerorsToBeAdded;i++){
				dotReplacedLatitude = dotReplacedLatitude.concat("0");
			}
			
		}
		Log.e(TAG, "After correction is.." + dotReplacedLatitude);
		return dotReplacedLatitude;
		
		
		
		
	}

}
